package com.hf.objio;

import java.io.*;

public class TestDriver {
    public static void main(String[] args) {
        test01();
    }
    /**
     * 对象流和序列化、transient关键字
     */
    public static void test01(){
        Teacher teacher = new Teacher("张三","ds",98);

        try {
            // 在这个时间阶段，将teacher对象的内存信息保存到文件对象中
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    new FileOutputStream("E:\\filedemo\\tt\\sse.txt"));
            //写出对象
            objectOutputStream.writeObject(teacher);
            System.out.println("将teacher对象从内存中写出到文件了");
            System.out.println(teacher);
            System.out.println("-----------------------------");

            ObjectInputStream objectInputStream = new ObjectInputStream(
                    new FileInputStream("E:\\filedemo\\tt\\sse.txt"));
            //读入对象流
            Object objIo = objectInputStream.readObject();
            //强制转换teacher类型
            Teacher tea = (Teacher) objIo;
            System.out.println("读入对象流");
            System.out.println(tea);

            // 写出的teacher对象和读入对象流对象不是同一个对象
            System.out.println("teacher == objIn:" + (teacher == tea));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
